﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL; 

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class ES_Inquire : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        //IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        private IJobSiteBLL jobBLL = new JobSiteBLL();
        private ES_InquireBLL obj = new ES_InquireBLL();
        private RadComboBox cmbJobsite;

        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            cmbJobsite = (RadComboBox)JobSiteCombo1.FindControl("cbJobSite");

            if (DataItem != null)
            {
                lblEmpSeq.Text = ssMng.EmpSeq;
                JobSiteCombo1.ListJobSiteComboBox(SystemInfo._JobsiteExternal);
                ListInqSubjectCombo();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblSequence.Text = DataBinder.Eval(DataItem, "inquire_dtls_seq").ToString();
                    txtNo.Text = DataBinder.Eval(DataItem, "seq").ToString();
                    txtReason.Text = DataBinder.Eval(DataItem, "reason").ToString();
                    cmbJobsite.SelectedValue = DataBinder.Eval(DataItem, "job_site_seq").ToString();
                    cmbInqSubjectCombo.SelectedValue = DataBinder.Eval(DataItem, "inquire_subject_seq").ToString();
                }
            }
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        public void ListInqSubjectCombo()
        {
            InquireBLL obj = new InquireBLL();

            DataTable dt = new DataTable();
            dt = obj.ListInqSubject();

            cmbInqSubjectCombo.Items.Clear();

            cmbInqSubjectCombo.DataSource = dt;
            cmbInqSubjectCombo.DataBind();
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {           

           int Year = Convert.ToInt32(Session["BgYear"].ToString());
           int EmpSeq = ssMng.EmpSeqI;
           int chkDupResult = obj.chkDup(int.Parse(lblEmpSeq.Text), Year);
           if (chkDupResult == 0)
           {
               // Insert table หลัก
               obj.Insert_hrm_es_inquires(Year
                   , EmpSeq
                   , 0
                   , EmpSeq);
               int InquireSeq = obj.GetInquireSeq(EmpSeq, Year);

               obj.Insert_hrm_es_inquire_dtls(InquireSeq
                   , int.Parse(cmbInqSubjectCombo.SelectedValue)
                   , int.Parse(txtNo.Text)
                   , int.Parse(cmbJobsite.SelectedValue)
                   , txtReason.Text.ToString()
                   , EmpSeq);                     
           }
           else
           {
               obj.Insert_hrm_es_inquire_dtls(chkDupResult
                   , int.Parse(cmbInqSubjectCombo.SelectedValue)
                   , int.Parse(txtNo.Text)
                   , int.Parse(cmbJobsite.SelectedValue)
                   , txtReason.Text.ToString()
                   , EmpSeq); 
              
          }
 
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {           

            obj.Update_hrm_es_inquire_dtls(Convert.ToInt32(lblSequence.Text.Trim())
                , Int32.Parse(cmbInqSubjectCombo.SelectedValue.ToString())
                , Int32.Parse(txtNo.Text)
                , Int32.Parse(cmbJobsite.SelectedValue)
                , txtReason.Text.Trim()
                , ssMng.EmpSeqI);      

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}